/**
 * 游戏主场境
 */
class MainMenu extends eui.Component {


	private static _ins: MainMenu;

	public static get ins(): MainMenu {
		if (!MainMenu._ins) {
			MainMenu._ins = new MainMenu();
		}
		return MainMenu._ins;
	}

	public constructor() {
		super();
		if (MainMenu._ins) {
			throw new Error("MainMenu 使用单例");
		}
		this.skinName = MainMenuSkin;
	}


	// 头部组
	public grpTop: eui.Group;
	// 底部组
	public grpBot: eui.Group;

	// 头像
	private imgFace: eui.Image;
	// 遮罩
	private rectFace: eui.Rect;
	// 昵称
	private labName: eui.Label;
	// 金币
	private labGold: eui.Label;
	// 提现
	private btnTixian: eui.Button;

	// 仓库
	private btnDepot: eui.Button;
	// 礼包
	private btnDaygift
	// 兑换
	private btnExchange
	// 采摘
	private btnGain
	// 播种
	private btnPlant: eui.Button;
	// 施肥
	private btnMarure

	// 多次分享
	private grpShare: eui.Group;
	private btnShare: eui.Group;

	private interval_id;
	private gain_interval_key = 0;

	// 分享参数
	private _share_type;
	private _share_callback;


	protected createChildren() {
		super.createChildren();

		// 赋值头像
		this.imgFace.source = PlayerSelfData.userinfo["avatar"];
		// 赋值昵称
		this.labName.text = PlayerSelfData.userinfo["nickname"];
		// 赋值金币
		this.goldChange();
		// 添加遮罩
		this.imgFace.mask = this.rectFace;
		this.addEvent();
	}



	/**
	 * 添加事件
	 */
	private addEvent() {
		// 头像
		this.imgFace.addEventListener(egret.TouchEvent.TOUCH_TAP, this.faceTap, this);
		// 提现
		this.btnTixian.addEventListener(egret.TouchEvent.TOUCH_TAP, this.tixianTap, this);
		// 仓库
		this.btnDepot.addEventListener(egret.TouchEvent.TOUCH_TAP, this.depotTap, this);
		// 礼包
		this.btnDaygift.addEventListener(egret.TouchEvent.TOUCH_TAP, this.daygiftTap, this);
		// 兑换
		this.btnExchange.addEventListener(egret.TouchEvent.TOUCH_TAP, this.exchangeTap, this);
		// 采摘
		this.btnGain.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gainTap, this);
		// 播种
		this.btnPlant.addEventListener(egret.TouchEvent.TOUCH_TAP, this.plantTap, this);
		// 施肥
		this.btnMarure.addEventListener(egret.TouchEvent.TOUCH_TAP, this.marureTap, this);
		// 监听金币改变事件
		EventManager.instance.addEvent(EventName.USER_GOLD_CHANGE, this.goldChange, this, true);
		// 分享
		this.btnShare.addEventListener(egret.TouchEvent.TOUCH_TAP, this.shareRepeatPage, this);
	}


	/**
	 * 改变金币
	 */
	private goldChange() {
		this.labGold.text = String(Number(PlayerSelfData.gold).toFixed(4));
	}


	/**
	 * 点击头像
	 */
	private faceTap() {
		SceneManager.ins.pushScene(new UserPanel);
	}


	/**
	 * 点击提现
	 */
	private tixianTap() {
		platform.skipExchange(4);
	}



	/**
	 * 点击 仓库
	 */
	private depotTap() {
		SceneManager.ins.pushScene(new DepotPanel);
	}



	/**
	 * 点击 礼包
	 */
	private daygiftTap() {
		SceneManager.ins.pushScene(new DaygiftPanel);
	}



	/**
	 * 点击 兑换
	 */
	private exchangeTap() {
		SceneManager.ins.pushScene(new ExchangePanel);
	}



	/**
	 * 点击 采摘
	 */
	private gainTap() {
		this.btnGain.enabled = false;
		this.gain_interval_key = 0;
		// this.interval_id = egret.setInterval(this.gainNet, this, 500);
		PlayerSelfData.landInfo((land_info) => {
			for (let i = 0; i < PlayerSelfData.landInfoLength; i++) {
				// 判断土地是否存在
				// 判断是否有植物
				// 判断是否有植物
				// 判断是否产出了果实
				//  &&
				// land_info[i]["have_num"] > 0
				if (
					typeof land_info[i] != "undefined" &&
					typeof land_info[i]["plant_id"] != "undefined" &&
					land_info[i]["plant_id"] != 0
				) {
					egret.setTimeout(function () {
						Net.ins.send(['harvest'], { "land_id": land_info[i]['id'] }, (e, c, k) => {
							// 播放采摘动画
							// PlayerSelfData.plant_gain = k;
							PlayerSelfData.plant_gain = PlayerSelfData.landInfoSolo(k)["id"];
							EventManager.instance.dispatch(EventName.PLANT_GAIN_CHANGE, k);

							PlayerSelfData.setLandInfo(k, e);
						}, i);
					}, this, this.gain_interval_key * 500)
					this.gain_interval_key++;
				}
			}
			this.gain_interval_key++;
			egret.setTimeout(function () {
				// 恢复按钮点击
				this.btnGain.enabled = true;
			}, this, this.gain_interval_key * 500)
		})
	}


	/**
	 * 采摘
	 */
	private gainNet() {
	}


	/**
	 * 点击 播种
	 */
	public plantTap() {
		// 弹出窗口
		SceneManager.ins.pushScene(new PlantPanel, "bottom", this.grpBot.height);
	}



	/**
	 * 点击 施肥
	 */
	private marureTap() {
		SceneManager.ins.pushScene(new ManurePanel);
	}



	/**
	 * 多次分享
	 * 
	 * @param type 类型
	 */
	public shareRepeat(type: number, callback: Function) {
		this.grpShare.visible = true;
		this._share_type = type;
		this._share_callback = callback;
		this.shareRepeatPage();
	}



	/**
	 * 页内多次分享
	 */
	private shareRepeatPage() {
		platform.wxshare(this._share_type, s => {
			if (s) {
				this.grpShare.visible = false;
				this._share_callback && this._share_callback(true);
			}
		})
	}

}